package com.sc.nft.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.*;
import com.sc.nft.entity.DigitalCollectionProductInfo;
import com.sc.nft.entity.SecondCollectionConfig;
import com.sc.nft.entity.ShareProfitPanel;
import com.sc.nft.enums.MarketLevelEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.ShareProfitPanelService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 服务接口实现
 *
 * @author zxy
 * @since 2023-12-09 15:58:57
 * @description 商户banner
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ShareProfitPanelServiceImpl implements ShareProfitPanelService {
    private final ShareProfitPanelDao shareProfitPanelDao;
    private final ShareProfitDao shareProfitDao;
    private final ShareProfitRatioDao shareProfitRatioDao;
    private final DigitalCollectionProductInfoDao digitalCollectionProductInfoDao;
    private final SecondCollectionConfigDao secondCollectionConfigDao;
    private final DigitalCollectionDao digitalCollectionDao;

    @Override
    public List<ShareProfitPanel> listTransPoolProfit() {
        return shareProfitPanelDao.findList(null, MarketLevelEnum.TRANS_POOL);
    }

    @Override
    public Page<ShareProfitPanel> panelPage(String panelName, MarketLevelEnum marketLevel, Boolean status, Page page) {
        return shareProfitPanelDao.panelPage(panelName, marketLevel, status, page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removePanel(Long id) {
        ShareProfitPanel panel = shareProfitPanelDao.getById(id);
        Assert.notNull(panel, () -> new GlobalRunTimeException("模板不存在"));
        switch (panel.getMarketLevel()) {
            case FIRST:
                DigitalCollectionProductInfo daoByPanelIdOne = digitalCollectionProductInfoDao.getByPanelIdOne(panel.getId());
                if (ObjectUtil.isNotNull(daoByPanelIdOne)) {
                    throw new GlobalRunTimeException(daoByPanelIdOne.getFirstTitle() + "配置了该模板，无法删除");
                }
                break;
            case SECOND:
                SecondCollectionConfig secondCollectionConfig = secondCollectionConfigDao.getByPanelIdOne(panel.getId());
                if (ObjectUtil.isNotNull(secondCollectionConfig)) {
                    throw new GlobalRunTimeException("二级市场：" + digitalCollectionDao.getFirstTitleById(secondCollectionConfig.getCollectionId()) + "配置了该模板，无法删除");
                }
                break;
        }
        shareProfitDao.clearByPanelId(panel.getId());
        shareProfitRatioDao.clearByPanelId(panel.getId());
        panel.deleteById();

    }

    @Override
    public List<ShareProfitPanel> findList(String panelName, MarketLevelEnum marketType) {
        return shareProfitPanelDao.findList(panelName, marketType);
    }

    @Override
    public ShareProfitPanel getById(Long id) {
        return shareProfitPanelDao.getById(id);
    }
}